package com.example.demo.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.example.demo.entity.*;
import com.example.demo.mapper.Jshop_brandMapper;
import com.example.demo.mapper.Jshop_goodsMapper;
import com.example.demo.mapper.Jshop_goods_typeMapper;
import com.example.demo.mapper.Jshop_imagesMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "首页controller", tags = {"进入首页获取相关资源的接口"})
@RestController
@RequestMapping("/login")
public class login {

    @Autowired
    private Jshop_imagesMapper imagesMapper;
    @Autowired
    private Jshop_brandMapper brandMapper;
    @Autowired
    private Jshop_goods_typeMapper typeMapper;
    @Autowired
    private Jshop_goodsMapper goodsMapper;

    @ApiOperation(value = "资源获取接口(banner图,及相关商品对象)")
    @GetMapping("/index")
    public Map<String, Object> index(){
        Map<String,Object> map = new HashMap<>();
        /**
         * 获取三个大banner
         */
        Jshop_images banner = imagesMapper.selectByName("时令水果");
        Jshop_images banner2 = imagesMapper.selectByName("清凉一夏");
        Jshop_images banner3 = imagesMapper.selectByName("春季新品");
        /**
         * 获取四个品牌及banner图片
         */
        Jshop_images brandbanner = imagesMapper.selectById(brandMapper.selectByName("三只松鼠").getLogo());
        Jshop_images brandbanner1 = imagesMapper.selectById(brandMapper.selectByName("辛巴克").getLogo());
        Jshop_images brandbanner2 = imagesMapper.selectById(brandMapper.selectByName("古驰").getLogo());
        Jshop_images brandbanner3 = imagesMapper.selectById(brandMapper.selectByName("sk-II").getLogo());

        Jshop_goods_type type = typeMapper.selectByName("集赞购");
        Jshop_goods_type type1 = typeMapper.selectByName("好物推荐");
        Jshop_goods_type type2 = typeMapper.selectByName("零食水果");

        List<Jshop_goods> list = new ArrayList<>();
               list = goodsMapper.selectList(new EntityWrapper<Jshop_goods>()
                .eq("goods_type_id",type.getId()));
        List<Jshop_goods> list1 = new ArrayList<>();
               list1 = goodsMapper.selectList(new EntityWrapper<Jshop_goods>()
                .eq("goods_type_id",type1.getId()));
        List<Jshop_goods> list2 = new ArrayList<>();
                list2 = goodsMapper.selectList(new EntityWrapper<Jshop_goods>()
                .eq("goods_type_id",type2.getId()));

        Index index = new Index();

        index.setBigbanner(banner);
        index.setBigbanner1(banner2);
        index.setBigbanner2(banner3);

        index.setBrandbanner(brandbanner);
        index.setBrandbanner1(brandbanner1);
        index.setBrandbanner2(brandbanner2);
        index.setBrandbanner3(brandbanner3);


        index.setGoods(new Goods(list.get(0),imagesMapper.selectById(list.get(0)).getUrl()));
        index.setGoods1(new Goods(list.get(1),imagesMapper.selectById(list.get(1)).getUrl()));
        index.setGoods2(new Goods(list.get(2),imagesMapper.selectById(list.get(2)).getUrl()));
        index.setGoods3(new Goods(list.get(3),imagesMapper.selectById(list.get(3)).getUrl()));

        index.setGoods4(new Goods(list1.get(0),imagesMapper.selectById(list1.get(0)).getUrl()));
        index.setGoods5(new Goods(list1.get(1),imagesMapper.selectById(list1.get(1)).getUrl()));
        index.setGoods6(new Goods(list1.get(2),imagesMapper.selectById(list1.get(2)).getUrl()));
        index.setGoods7(new Goods(list1.get(3),imagesMapper.selectById(list1.get(3)).getUrl()));

        index.setGoods8(new Goods(list2.get(0),imagesMapper.selectById(list2.get(0)).getUrl()));
        index.setGoods9(new Goods(list2.get(1),imagesMapper.selectById(list2.get(1)).getUrl()));
        index.setGoods10(new Goods(list2.get(2),imagesMapper.selectById(list2.get(2)).getUrl()));
        index.setGoods11(new Goods(list2.get(3),imagesMapper.selectById(list2.get(3)).getUrl()));

        map.put("object",index);
        return map;
    }

}
